package com.cnfangmao.masterdata.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.AccountRoleDto;
import com.cnfangmao.masterdata.dto.AccountRoleSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.AccountRole;
import com.cnfangmao.masterdata.service.AccountRoleService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 账号角色关联表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@RequestMapping("/accountRole")
@Api(tags="0-6、配置-用户账号角色关联管理")
@RestController
public class AccountRoleController {

private static final Logger logger =LoggerFactory.getLogger(RolePermissionController.class);
	
	@Autowired
	private AccountRoleService accountRoleService;
	
	@PostMapping("/list.do")
	@ApiOperation(value="查找账号角色关联列表")
	@ApiImplicitParam(name="dto",value="查找账号角色关联列表条件",paramType="body",required=true,dataType="GeneralSearchDto«AccountRoleSearchDto»")
	public ResBody<GeneralPageVo<AccountRole>> listAccountRole(@RequestBody GeneralSearchDto<AccountRoleSearchDto> dto) {
		ResBody<GeneralPageVo<AccountRole>> resBody = new ResBody<GeneralPageVo<AccountRole>>();
		try {
			IPage<AccountRole> accountRole =accountRoleService.queryListAccountRole(dto);
			GeneralPageVo<AccountRole> result= new GeneralPageVo<AccountRole>();
			result.setPageNo(accountRole.getCurrent());
			result.setPageSize(accountRole.getPages());
			result.setRecords(accountRole.getRecords());
			result.setTotalCount(accountRole.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in AccountRoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/create.do")
	@ApiOperation(value="创建账号角色关联信息")
	@ApiImplicitParam(name="dto",value="创建账号角色关联信息",paramType="body",required=true,dataType="AccountRoleDto")
	public ResBody<Boolean> createAccountRole(@RequestBody AccountRoleDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getUserId().isEmpty() || dto.getRoleId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色id或者用户id为空");
				return resBody;
			}
			if(accountRoleService.findByUserIdAndRoleId(dto.getUserId(), dto.getRoleId())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("账号角色关联已经存在");
				return resBody;
			}
			dto.setUserRoleId(SnowFlakeUtils.generator());
			AccountRole accountRole =ModelUtils.parseObject(dto, AccountRole.class, "create");
			accountRoleService.save(accountRole);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run create error in AccountRoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;		
	}
	
	@PostMapping(value="/update.do")
	@ApiOperation(value="修改账号角色关联信息")
	@ApiImplicitParam(name="dto",value="修改账号角色关联信息",paramType="body",required=true,dataType="AccountRoleDto")
	public ResBody<Boolean> updateAccountRole(@RequestBody AccountRoleDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getUserId().isEmpty() || dto.getRoleId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色id或者用户id为空");
				return resBody;
			}
			AccountRole accountRole =accountRoleService.findByUserIdAndRoleId(dto.getUserId(), dto.getRoleId());
			if(accountRole!=null && !accountRole.getUserRoleId().equals(dto.getUserRoleId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("账号角色关联已经存在");
				return resBody;
			}
			AccountRole entity =ModelUtils.parseObject(dto, AccountRole.class, "update");
			accountRoleService.updateById(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run update error in AccountRoleController： %s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除账号角色关联信息")
	@ApiImplicitParam(name="userRoleId",value="删除账号角色关联 userRoleId",paramType="body",required=true,dataType="string")
	public ResBody<Boolean> deleteAccountRole(@RequestBody String userRoleId) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			accountRoleService.removeById(userRoleId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run delete error in AccountRoleController : %s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/getAccountRole.do")
	@ApiOperation(value="获取账号角色关联信息")
	@ApiImplicitParam(name="userRoleId",value="获取账号角色关联 userRoleId",paramType="body",required=true,dataType="string")
	public ResBody<AccountRole> getAccountRole(@RequestBody String userRoleId) {
		ResBody<AccountRole>  resBody = new ResBody<AccountRole>();
		try {
			AccountRole accountRole =accountRoleService.getById(userRoleId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(accountRole);
		} catch (Exception e) {
			logger.error(String.format("run getAccountRole error in AccountRoleController :%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	 /**
     * 获取所有用户权限对应数据
     */
	@PostMapping(value="/getAccountRoleByUserId.do")
    @ApiOperation(value="获取所有用户权限对应数据", notes = "")
    @ApiImplicitParam(name = "userId", value = "用户userId", paramType="body",required = true, dataType = "string")
    public ResBody<List<AccountRole>> getAccountRoleByUserId(@RequestBody String userId){
		ResBody<List<AccountRole>> resBody = new ResBody<List<AccountRole>>();
    	try {
        	List<AccountRole> accountRoles=accountRoleService.findListByUserId(userId);   
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(accountRoles);
		} catch (Exception e) {
			logger.error(String.format("run getAccountRoleByUserId error in AccountRoleController :%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
}

